package com.amtf.datafile2.core.txt;

import com.amtf.datafile2.DataException;
import com.amtf.datafile2.DataField;
import com.amtf.datafile2.DataFieldType;

public class TxtFTAlphabetNumeric extends TxtFTBasic {

	public static final DataFieldType instance = new TxtFTAlphabetNumeric("Alphabet or Numeric");

	public TxtFTAlphabetNumeric(String name) {
		super(name);
	}

	public TxtFTAlphabetNumeric(int minLength, int maxLength) {
		super(instance.getName(), minLength, maxLength);
	}

	public void validate(DataField value) throws DataException {
		super.validate(value);

		String tmpValue = ((String) value.getValue()).trim().toLowerCase();
		for (int i = 0; i < tmpValue.length(); i++) {
			char c = tmpValue.charAt(i);
			if (c != '.' && c != '-' && !(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9')) {
				throw new DataException("Value type not match with definition:" + name);
			}
		}
	}
}
